import API from "../config/API";

/**
 * 网络请求封装
 * @param {String} url 请求路径
 * @param {Object} data 请求携带的数据
 * @param {String} method 请求方式，默认为 GET
 */
export default (url, data = {}, method = 'GET') => {
    return new Promise((resolve, reject) => {
        let cookies = wx.getStorageSync('cookies');
        let cookie = '';
        if (cookies) {
            cookie = cookies.find(item => item.indexOf('MUSIC_U') !== -1)
        }
        wx.request({
            url: API.baseUrl + url,
            data,
            method,
            header: { cookie },
            success(resp) {
                // 判断是否是登录请求
                if (data.isLogin) {
                    wx.setStorage({ key: 'cookies', data: resp.cookies });
                }
                resolve(resp.data);
            },
            fail(err) {
                reject(err);
            }
        });
    });
}